package org.imlinker.gui;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

@SuppressWarnings("serial")
public class ChatWindow extends TextAreaSupportWinodw {

	public ChatWindow(final Chat paraChat, final String name, final String m,
			final String linkToLabel) {
		this.chat = paraChat;

		initComponents();

		initActions();

		if (m != null) {
			displayArea.append(m + "\n");
			displayArea.setCaretPosition(displayArea.getText().length());
		}

		setTitle(linkToLabel + name);
		setName(name);
		restoreLocation();

	}

	private void initActions() {
		// 监听发来的信息
		chat.addMessageListener(new MessageListener() {
			public void processMessage(final Chat c, final Message m) {

				if (m.getBody() == null) {
					return;
				}
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						displayArea.append(m.getBody() + "\n");
						displayArea.setCaretPosition(displayArea.getText()
								.length());
					}
				});
			}
		});

		chatArea.addKeyListener(new KeyAdapter() {
			public void keyPressed(final KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {

					sendMessage(chatArea.getText());
					chatArea.setText("");
				}
			}

			public void keyReleased(final KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {

					if (chatArea.getText().charAt(0) == '\n') {
						chatArea.setCaretPosition(0);
						chatArea.setText("");
					}
				}
			}

		});

		chatArea.addMouseListener(this);
		displayArea.addMouseListener(this);
	}

	public void active() {
		chatArea.requestFocus();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
		chatArea = new javax.swing.JTextArea();
		JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
		displayArea = new javax.swing.JTextArea();

		chatArea.setColumns(20);
		chatArea.setLineWrap(true);
		chatArea.setRows(2);

		jScrollPane1.setViewportView(chatArea);

		displayArea.setColumns(20);
		displayArea.setEditable(false);
		displayArea.setLineWrap(true);
		displayArea.setRows(8);
		jScrollPane2.setViewportView(displayArea);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 356,
				Short.MAX_VALUE).addComponent(jScrollPane2,
				javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addComponent(jScrollPane2,
						javax.swing.GroupLayout.DEFAULT_SIZE, 243,
						Short.MAX_VALUE).addPreferredGap(
						javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.PREFERRED_SIZE)));

		pack();
	}// </editor-fold>

	/**
	 * 发送消息的函数，修改两种情况，消息为空时直接退出，最后一个字符是回车符时，删除最后一个字符.
	 * 
	 * 同时把发送出去的消息也显示在聊天区域，跟接受到的信息差别是加上了一组括号。
	 * 
	 * @param message
	 */
	private void sendMessage(final String paraMessage) {
		String message = paraMessage;

		// 剔除消息为空的情况
		if (message.length() == 0) {
			return;
		}

		// 删除消息末尾可能存在的回车符
		if (message.charAt(message.length() - 1) == '\n') {
			message = message.substring(0, message.length() - 1);
		}

		try {
			chat.sendMessage(message);
			displayArea.append("------" + message + "\n");
			displayArea.setCaretPosition(displayArea.getText().length());
		} catch (XMPPException e) {
			e.printStackTrace();
		}
	}

	// Variables declaration - do not modify

	private javax.swing.JTextArea displayArea;

	private javax.swing.JTextArea chatArea;

	// End of variables declaration

	private Chat chat;

}
